package ps;

import com.rabbitmq.client.*;
import util.ConnectionRabbitMQ;

import java.io.IOException;

/**
 * 消息消费者1
 */
public class Recer1 {
    public static void main(String[] args) throws Exception{
        // 1.获得连接
        Connection connection = ConnectionRabbitMQ.getConnection();
        // 2.在连接中创建通道
        Channel channel = connection.createChannel();
        // 声明队列
        channel.queueDeclare("test_exchange_fanout_queue_1",false,false,false,null);
        // 绑定路由
        channel.queueBind("test_exchange_fanout_queue_1","test_exchange_fanout","");
        // 3.从信道中获得信息
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                String s = new String(body);
                System.out.println("【消费者1】= :"+s);
            }
        };
        // 4.监听队列
        channel.basicConsume("test_exchange_fanout_queue_1",true,consumer);

    }
}
